package com.example.bluetooth

import android.Manifest
import android.bluetooth.BluetoothAdapter
import android.bluetooth.BluetoothManager
import android.content.Intent
import android.content.pm.PackageManager
import android.os.Build

import android.os.Bundle
import android.view.LayoutInflater
import android.widget.Toast
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import com.example.bluetooth.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity() {
    private lateinit var requestBTEnable: ActivityResultLauncher<String>
    private val bluetoothManager by lazy {
        getSystemService(BLUETOOTH_SERVICE) as BluetoothManager
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val activityMainBinding = ActivityMainBinding.inflate(LayoutInflater.from(this))
        setContentView(activityMainBinding.root)
        requestBTEnable =
            registerForActivityResult(ActivityResultContracts.RequestPermission()) { granted ->
                if (granted) {
                    // 开启蓝牙
                    openBluetooth()
                } else {
                    // 引导去设置页面, 手动开启蓝牙
                    if (!shouldShowRequestPermissionRationale(Manifest.permission.BLUETOOTH_CONNECT)) {
                        val dialog = BluetoothSettingsDialog()
                        dialog.showDialog(supportFragmentManager)
                    }
                }
            }
        activityMainBinding.openBluetooth.setOnClickListener {
            if (bluetoothManager.adapter == null) {
                Toast.makeText(this, "设备不支持蓝牙", Toast.LENGTH_SHORT).show()
                return@setOnClickListener
            }
            if (!bluetoothManager.adapter.isEnabled) {
                // 蓝牙已开启
                if (ActivityCompat.checkSelfPermission(
                        this, Manifest.permission.BLUETOOTH_CONNECT
                    ) == PackageManager.PERMISSION_GRANTED
                ) {
                    Toast.makeText(this, "蓝牙已开启", Toast.LENGTH_SHORT).show()
                    return@setOnClickListener
                }
                // 展示蓝牙权限申请说明
                if (shouldShowRequestPermissionRationale(Manifest.permission.BLUETOOTH_CONNECT)) {
                    val dialog = BluetoothPermissionDialog().apply {

                        allowCallback = {
                            dismiss()
                            // 申请开启蓝牙的权限
                            requestBluetoothPermission()
                        }
                    }
                    dialog.showDialog(supportFragmentManager)
                    return@setOnClickListener
                }
                // 申请开启蓝牙的权限
                requestBluetoothPermission()
            }
        }
    }

    private fun requestBluetoothPermission() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
            requestBTEnable.launch(Manifest.permission.BLUETOOTH_CONNECT)
        }
    }


    private fun openBluetooth() {
        val intent = Intent()
        intent.setAction(BluetoothAdapter.ACTION_REQUEST_ENABLE)
        startActivity(intent)
        Toast.makeText(this, "蓝牙已开启", Toast.LENGTH_SHORT).show()
    }
}